<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\circle;

use app\api\model\dao\Circle;
use app\api\model\dao\UpRank;
use app\api\model\dao\UserFollow;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Log;
use think\Validate;

class FollowCircle extends PageBase
{

    private $objCircle;
    private $objUserFollow;
    private $objUpRank;

    public function __construct()
    {
        parent::__construct();

        $this->objCircle = new Circle();
        $this->objUserFollow = new UserFollow();
        $this->objUpRank = new UpRank();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'cir_id' => 'require',
        ];
        $msg = [
            'cir_id.require' => '请选择关注的圈子',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function myExcute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            throw new Exception('check token fail...', Error::ERR_NO_AUTH);
        }

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            throw new Exception($ret, Error::ERR_PARAM);
        }

        // 2、从数据库中获取圈子详情
        $ret = $this->getCircleInfoFromDB($param['cir_id']);
        if (false === $ret) {
            throw new Exception('get circle info from db fail. [circle_id: ' . $param['cir_id'] . ']', Error::ERR_SYS);
        }

        if (empty($ret)) {
            throw new Exception('circle is empty. [circle_id: ' . $param['cir_id'] . ']', Error::ERR_CIRCLE_NOT_EXIST);
        }

        // 3、校验是否被删除
        if (Flag::CONST_CIRCLE_DELETED == $ret['is_delete']) {
            throw new Exception('circle is deleted', Error::ERR_CIRCLE_DELETED);
        }

        // 4、校验是否审核
        if (Flag::CONST_CIRCLE_APPROVE_PASS != $ret['approve_status']) {
            throw new Exception('circle is not approve', Error::ERR_CIRCLE_APPROVE_NOT);
        }

        $follow_ret = $this->objUserFollow->isFollow($this->user_id, Flag::CONST_FOLLOW_CIRCLE, $param['cir_id']);
        if (false === $follow_ret) {
            throw new Exception('get user follow fail.', Error::ERR_SYS);
        }

        if ($follow_ret > 0) {
            throw new Exception('get user follow fail.', Error::ERR_USER_FOLLOW_CIRCLE);
        }

        $input = [
            'userid' => $this->user_id,
            'follow_type' => Flag::CONST_FOLLOW_CIRCLE,
            'follow_id' => $param['cir_id'],
            'date' => date('Ymd'),
            'create_time' => time()
        ];
        $this->objUserFollow->follow($input);

        $up_rank_ret = $this->objUpRank->plusUp($ret['creator'], date('Ymd'));
        if (false === $up_rank_ret) {
            Log::error(' add consumer rank fail. [userid:' . $ret['creator'] . '][date:' . date('Ymd') . ']');
        }
    }

    /**
     * 从数据库中获取圈子详情
     *
     * @param $cir_id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getCircleInfoFromDB($cir_id)
    {
        if (!$cir_id) {
            return false;
        }
        $arrCond = [
            'id' => ['=', $cir_id]
        ];
        $ret = $this->objCircle->getCircle($arrCond);
        if (false === $ret) {
            return false;
        }
        $ret = $ret[0];
        return $ret;
    }
}
